<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="PATH是一个系统的环境变量，它用法很多人都了解，是指定命令的搜索路径。本例子在Linux下讲解。比如说，在&#x2F;opt&#x2F;games&#x2F;csgo文件夹下有一个名为playcsgo的可执行文件，作用是启动游戏。那么，假如你需要运行它，你必需要输入绝对路径，例如： 1&#x2F;opt&#x2F;games&#x2F;csgo&#x2F;playcsgo 此时，不出问题的话，游戏已经开始了。但是，路径是很繁长的，每次都要用绝对路径来运行，绝对是个">
<meta property="og:type" content="article">
<meta property="og:title" content="PATH环境变量的介绍">
<meta property="og:url" content="http://example.com/2021/08/26/path-env/index.html">
<meta property="og:site_name" content="Maicss">
<meta property="og:description" content="PATH是一个系统的环境变量，它用法很多人都了解，是指定命令的搜索路径。本例子在Linux下讲解。比如说，在&#x2F;opt&#x2F;games&#x2F;csgo文件夹下有一个名为playcsgo的可执行文件，作用是启动游戏。那么，假如你需要运行它，你必需要输入绝对路径，例如： 1&#x2F;opt&#x2F;games&#x2F;csgo&#x2F;playcsgo 此时，不出问题的话，游戏已经开始了。但是，路径是很繁长的，每次都要用绝对路径来运行，绝对是个">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://z3.ax1x.com/2021/08/26/hnHs6x.png">
<meta property="article:published_time" content="2021-08-26T09:14:56.000Z">
<meta property="article:modified_time" content="2021-11-16T07:49:25.342Z">
<meta property="article:author" content="Maicss">
<meta property="article:tag" content="环境变量">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://z3.ax1x.com/2021/08/26/hnHs6x.png">


<link rel="canonical" href="http://example.com/2021/08/26/path-env/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2021/08/26/path-env/","path":"2021/08/26/path-env/","title":"PATH环境变量的介绍"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>PATH环境变量的介绍 | Maicss</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Maicss</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#PATH%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E7%9A%84%E9%80%9A%E5%B8%B8%E7%94%A8%E6%B3%95"><span class="nav-number">1.</span> <span class="nav-text">PATH环境变量的通常用法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%89%E4%BA%9B%E9%97%AE%E9%A2%98%E9%9C%80%E8%A6%81%E6%B3%A8%E6%84%8F%E4%B8%80%E4%B8%8B"><span class="nav-number">2.</span> <span class="nav-text">有些问题需要注意一下</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Maicss</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">13</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/08/26/path-env/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Maicss">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Maicss">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          PATH环境变量的介绍
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-08-26 17:14:56" itemprop="dateCreated datePublished" datetime="2021-08-26T17:14:56+08:00">2021-08-26</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-11-16 15:49:25" itemprop="dateModified" datetime="2021-11-16T15:49:25+08:00">2021-11-16</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>PATH是一个系统的环境变量，它用法很多人都了解，是指定命令的搜索路径。<br>本例子在Linux下讲解。<br>比如说，在<code>/opt/games/csgo</code>文件夹下有一个名为<code>playcsgo</code>的可执行文件，作用是启动游戏。<br>那么，假如你需要运行它，你必需要输入绝对路径，例如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/opt/games/csgo/playcsgo</span><br></pre></td></tr></table></figure>
<p>此时，不出问题的话，游戏已经开始了。<br>但是，路径是很繁长的，每次都要用绝对路径来运行，绝对是个麻烦事。<br>此时PATH环境变量的作用就显现出来了，我希望实现输入<code>playcsgo</code>即可开始游戏，无需前边冗长的路径信息。<br>所以，此时只需要输入以下命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># export命令的作用是设置环境变量的值</span></span><br><span class="line"><span class="built_in">export</span> PATH=/opt/games/csgo</span><br></pre></td></tr></table></figure>
<p>先将PATH环境变量的值赋值为对应可执行文件所在的目录，后紧接着输入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">playcsgo</span><br></pre></td></tr></table></figure>
<p>此时就可以实现省略前边的路径部分了，因为你输入了一个命令，命令就是一个可执行程序，系统会自动在PATH所提供的路径中寻找和你输入命令的名字一致的可执行文件，然后对应执行。</p>
<h2 id="PATH环境变量的通常用法"><a href="#PATH环境变量的通常用法" class="headerlink" title="PATH环境变量的通常用法"></a>PATH环境变量的通常用法</h2><p>其实系统中默认的PATH是有值的，在Linux下，通常包括<code>/usr/bin</code>，那么对Linux不那么熟悉的可以去看一下，平时用的非常频繁的命令，比如<code>cd</code>,<code>mkdir</code>,<code>rm</code>等，都可以在这个目录下找到对应的可执行文件。<br>但是，值得注意的是，PATH中，允许放入多个路径，不仅PATH，其他环境变量也是这样。<br>在Linux操作系统下，不同的值以<code>英文冒号&quot;:&quot;</code>分割，Windows下以<code>英文分号&quot;;&quot;</code>分割。<br>例如：<br>当你输入了上边提到的<code>export PATH=/opt/games/csgo</code>命令后，你将无法使用<code>cd</code>,<code>mkdir</code>等这些命令了，因为你将PATH重新赋值后，其中不再包含<code>/usr/bin</code>,而这些常用命令通常就是在这个目录下，所以会报错显示没有找到命令或可执行文件。<br>那么如何解决?</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 如上所说，两个路径以英文分号隔开</span></span><br><span class="line"><span class="built_in">export</span> PATH=/usr/bin:/opt/games/csgo</span><br></pre></td></tr></table></figure>
<p>但是还是产生了一个问题，假如原来的PATH不止有/usr/bin呢，可能还包含其他路径，比如Java的JDK，此时如果这么做，其他的路径就会失效。<br>所以，下面这个方法，才是最最最正确的用法</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># $PATH表示取的环境变量PATH的值</span></span><br><span class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:/opt/games/csgo</span><br></pre></td></tr></table></figure>
<p>这里的意思是追加<code>/opt/games/csgo</code>到PATH原有值的末尾。<br>如图所示：<br><img src="https://z3.ax1x.com/2021/08/26/hnHs6x.png"><br>这时既不影响原有变量的值，还能放心的添加自己想要的路径。</p>
<h2 id="有些问题需要注意一下"><a href="#有些问题需要注意一下" class="headerlink" title="有些问题需要注意一下"></a>有些问题需要注意一下</h2><blockquote>
<p>Q:假如PATH变量中，多个路径有相同名称的可执行文件，那么它要优先选择哪个？<br>A:优先选择路径位于PATH变量靠前的位置的，从上边图中可以看到，PATH变量的储存方式是一串字符串，其中以:隔开，这表示他们是有顺序之分的，系统会优先选择位置靠前的值。</p>
</blockquote>
<blockquote>
<p>Q:在终端中输入上述设置变量的命令，会一直有效吗？<br>A:不会，只在此刻此终端下有效，其他终端无效，并且当你关掉此终端时，你所设置的将丢失；在shell脚本中也是，只在脚本的运行范围内有效。</p>
</blockquote>
<blockquote>
<p>Q:如何设置长期有效的环境变量？<br>A:Windows下很容易，有变量的设置界面，Linux下可以在各种开机自启动脚本中写入设置命令。</p>
</blockquote>
<blockquote>
<p>Q:有些软件需要特定版本的JRE，我怎么让他们所使用的JAVA版本各不相同？<br>A:首先你必须准备所有所需要的版本的JRE，把他们放在不同的文件夹下，然后对每个程序编写运行脚本，shell脚本只在脚本的范围内有效，所以这种方式不会影响到其他程序。<br>脚本内容示例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 假定该程序所需JRE的路径为：/usr/jre8</span></span><br><span class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:/usr/jre8/bin <span class="comment">#加bin是因为jre的可执行文件通常在安装目录的bin目录下</span></span><br><span class="line">java playcsgo <span class="comment">#这个命令是执行你想要用上边设置的jre运行的java程序。</span></span><br></pre></td></tr></table></figure>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F/" rel="tag"># 环境变量</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/08/26/qt-smooth-scroll/" rel="prev" title="Qt列表等控件实现平滑滚动&deepin启动器存在的问题">
                  <i class="fa fa-chevron-left"></i> Qt列表等控件实现平滑滚动&deepin启动器存在的问题
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/08/29/deepin-wiki/" rel="next" title="一个中学生搞的wiki">
                  一个中学生搞的wiki <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Maicss</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
